import { getToken, removeToken, setToken } from '@/utils/auth'
import { getInfo, getInfoid, login } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updatetoken(state, token) {
      state.token = token
      setToken(token)
    },
    updateuserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    reToken(state) {
      state.token = ''
      removeToken()
    },
    reUserinfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async  userLogin(context, loginForm) {
      const res = await login(loginForm)
      context.commit('updatetoken', res.data)
      return res
    },
    async postUserInfo(context) {
      const res = await getInfo()
      const res2 = await getInfoid(res.data.userId)
      context.commit('updateuserInfo', { ...res.data, ...res2.data })
      return { ...res.data, ...res2.data }
    },
    logout(context) {
      context.commit('reToken')
      context.commit('reUserinfo')
    }

  },
  getters: {}
}
